package tv.huan.media.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tv.huan.common.core.text.Convert;
import tv.huan.media.domain.Team;
import tv.huan.media.domain.TeamUser;
import tv.huan.media.mapper.TeamMapper;
import tv.huan.media.mapper.TeamUserMapper;
import tv.huan.media.service.ITeamService;
import tv.huan.system.mapper.SysUserMapper;

/**
 * 审核小组Service业务层处理
 * 
 * @author admin
 * @date 2020-04-02
 */
@Service
public class TeamServiceImpl implements ITeamService 
{
    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TeamUserMapper teamUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询审核小组
     * 
     * @param id 审核小组ID
     * @return 审核小组
     */
    @Override
    public Team selectTeamById(Long id)
    {
        return teamMapper.selectTeamById(id);
    }

    /**
     * 查询审核小组列表
     * 
     * @param team 审核小组
     * @return 审核小组
     */
    @Override
    public List<Team> selectTeamList(Team team)
    {
        return teamMapper.selectTeamList(team);
    }

    /**
     * 新增审核小组
     * 
     * @param team 审核小组
     * @return 结果
     */
    @Override
    public int insertTeam(Team team)
    {
        return teamMapper.insertTeam(team);
    }

    /**
     * 修改审核小组
     * 
     * @param team 审核小组
     * @return 结果
     */
    @Override
    public int updateTeam(Team team)
    {
        return teamMapper.updateTeam(team);
    }

    /**
     * 删除审核小组对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTeamByIds(String ids)
    {
        return teamMapper.deleteTeamByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除审核小组信息
     * 
     * @param id 审核小组ID
     * @return 结果
     */
    public int deleteTeamById(Long id)
    {
        return teamMapper.deleteTeamById(id);
    }

    /**
     * 分配审核人员
     *
     * @param teamId 审核小组ID
     * @param userIds 用户ID
     * @return 结果
     */
    @Transactional
    public void assignUser(Integer teamId, String userIds){
        Long[] userIdArr = Convert.toLongArray(userIds);
        TeamUser teamUser;
        for (Long userId : userIdArr){
            teamUser = new TeamUser();
            teamUser.setTeamId(teamId);
            teamUser.setUserId(userId);
            teamUserMapper.insertTeamUser(teamUser);
            sysUserMapper.updateRemark(userId,"1");
        }
    }

    /**
     * 删除分配审核人员
     *
     * @param teamId 审核小组ID
     * @param userIds 用户ID
     * @return 结果
     */
    @Transactional
    public void deleteAuditUserList(Integer teamId, String userIds){
        Long[] userIdArr = Convert.toLongArray(userIds);
        for (Long userId : userIdArr){
            teamUserMapper.deleteAuditUserList(teamId,userId);
            sysUserMapper.updateRemark(userId,"0");
        }
    }
}
